package com.example.progressrecord.leancloud

import cn.leancloud.AVObject
import cn.leancloud.AVQuery
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

object ProgressInfoRepository {

    suspend fun ProgressInfo.save() = withContext(Dispatchers.IO) {
        return@withContext kotlin.runCatching {
            this@save.getObj().saveInBackground().blockingFirst()
        }.getOrNull()
    }


    suspend fun queryByTimeDesc(): List<AVObject> = withContext(Dispatchers.IO) {
        return@withContext runCatching {
            AVQuery<AVObject>(ProgressInfo.NAME)
                .orderByDescending(ProgressInfo.TIME_KEY)
                .find()
        }.getOrElse { emptyList() }
    }

}